<?php

class BooksController extends Controller
{
	public function actionCheckin()
	{
		$this->render('checkin');
	}

	public function actionCheckout()
	{
		$this->render('checkout');
	}

	public function actionIndex()
	{
		$this->render('list');
	}

	public function actionNew()
	{
		$this->render('form');
	}

	public function actionView($field, $value)
	{	
		//$book = $this->searchBooks($field, $value);
		$view = array(
			'id' => 'dw001',
			'title' => 'Tha Best Book Eva!',
			'author' => 'Derrick W',
			'status' => 'Out',
			'actionHref' => $this->createUrl('/books/checkin/', array('bid' => 'dw001')),
			'actionLabel' => 'Check In'
		);
		$this->render('view', $view);
	}

	private function searchBooks($field, $value)
	{
		if (!$this->isValidSearchField($field)) 	
			throw new Exception('You can not search books by [' . $field . '] field');
		
		$criteria = array();
		$criteria['where'] = "{$field} = '{$value}'";
		
		$book = Book::model()->find($criteria);
		return $book;
	}
	
	private function isValidSearchField($field) 
	{
		$validFields = array('id', 'title', 'description');
		return in_array($field, $validFields);
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}